package com.atguigu.edumsm.controller;

import com.atguigu.commonutils.R;
import com.atguigu.edumsm.service.MsmService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 25169
 */
@RestController
@RequestMapping("/edumsm/msm")
public class MsmController {

    @Autowired
    private MsmService msmService;

    @Autowired
    private RedisTemplate<String,String > redisTemplate;

    /**
     * 发送短信的方法
     * @param phone
     * @return
     */
    @GetMapping("send/{phone}")
    public R sendMsm(@PathVariable String phone){
        //1.从redis获取验证码，如果获取到直接返回
        String code=redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(code)){
            return R.ok();
        }

        //若Redis中没有，调用service发送短信方法
        boolean res = msmService.send(phone);

        if (res){
            return R.ok();
        }else {
            return R.error().message("短信发送失败");
        }

    }
}
